// pages/searchMap/searchMap.js

import {
    getbaseUrl,
    requestUtil
} from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isInfoCard:false,
        hei: 0,
        value: "",
        timer: null,
        searchData: [],
        filterData: [],
        latitude: "22.830777",
        longitude: "108.191033",
        baseUrl:"",
        map: {
            longitude: "108.191033",
            latitude: "22.830777",
            marker: []
        },
        mapInfoObj:{

        }
    },
    // 关闭地图卡
    closeInfoCard(){
        // console.log("111")
       if(this.data.isInfoCard)
       {
        this.setData({
            isInfoCard:false
        })
       }
       
    },
    // 展现信息卡
    showMarket(e){
        setTimeout(()=>{
            // console.log("2222")
       let markList = e.currentTarget.dataset.markerinfo
       let markId = (e.detail.markerId).toString()
        if(markId.length==5)return
       let resultArr = markList.filter(item=>{
           return item.id ==markId
       })
       let obj = resultArr[0]
       let storeName = "宏鼎烘焙"+(obj.storeName.substring(4))
    // console.log(obj.storeName.substring(4))
       this.setData({
         mapInfoObj:{...obj,storeName},
         isInfoCard:true
       })
        },200)
    },

    // 跳转到位置地图
    toMM(e){
        console.log(e.currentTarget.dataset)
    let {
        latitude,
        longitude
      } = e.currentTarget.dataset
      wx.openLocation({
        latitude: parseFloat(latitude),
        longitude: parseFloat(longitude),
        success: (res) => {
          // console.log(res)
        },
        fail: (err) => {
          // console.log(err)
        }
      })
    },

    // 去打电话
    toCall(e){
        // console.log(e.currentTarget.dataset.phone)
        let phone = e.currentTarget.dataset.phone
          wx.makePhoneCall({
            phoneNumber: phone,
            success: (res) => {
              console.log(res)
            },
            fail: (err) => {
              console.log(err)
            }
          })
    },



    // 清空输入值
    clearAll() {
        // console.log("fefse")
        this.setData({
            value: ""
        })
    },
    // 搜索请求
    search(e) {
        // if(!this.data.value)return
        var QQMapWX = require('../../utils/qqmap-wx-jssdk1.1/qqmap-wx-jssdk');

        let qqmapsdk = new QQMapWX({
            key: 'AVLBZ-OBU66-J2VS6-EBTPX-RX4H7-QDFI4'
        });
        let value = e.detail.value
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(() => {
            qqmapsdk.getSuggestion({
                keyword: value,
                region: "南宁市",
                success: (res) => {
                    // console.log(res.data);
                    this.filterData(res.data)
                    this.setData({
                        searchData: res.data
                    })
                },
                fail: (res) => {
                    // console.log(res);
                    this.filterData([])
                    this.setData({
                        searchData: []
                    })
                },
            })
            this.setData({
                value: value
            })

        }, 1000)

    },

    // 返回
    back(){
        wx.navigateBack({
          delta: 1,
        })
    },

    // 我的位置
    myPlaction(){
        let mapContent = wx.createMapContext("map", this)

        mapContent.moveToLocation({
            longitude:this.data.longitude,
            latitude:this.data.latitude
        })
    },

    // 过滤数据
    filterData(data) {
        let newArr = []
        data.forEach(item => {
            newArr.push({
                title: item.title,
                address: item.address,
                location: item.location
            })
        })
        this.setData({
            filterData: newArr
        })
        // console.log(newArr)
    },

    // 获取到数据
    getAddress(e) {
        let item = e.currentTarget.dataset.item
        //   console.log(item)
        let random = "12" + Math.ceil(Math.random() * (1000 - 1) + 1)
        let arr = this.data.map.marker
        arr.push({
            latitude: item.location.lat,
            longitude: item.location.lng,
            id: parseInt(random),
            width: 50,
            address: item.address,
            height: 50
        })

        this.setData({
            value: "",
            "map.longitude": item.location.lng,
            "map.latitude": item.location.lat,
            "map.marker": arr

        })
    },

    // 获取用户的地址
    getUserAddress() {
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
                const latitude = res.latitude
                const longitude = res.longitude
                // console.log(res)
                let random = "12" + Math.ceil(Math.random() * (1000 - 1) + 1)
                // console.log(parseInt(random))
                let arr = this.data.map.marker
                arr.push({
                    latitude: latitude.toString(),
                    longitude: longitude.toString(),
                    id: parseInt(random),
                    width: 50,
                    address: "南宁职业技术学院",
                    height: 50
                })

                this.setData({
                    latitude: latitude,
                    longitude: longitude,
                    "map.latitude": latitude,
                    "map.longitude": longitude,
                    "map.marker": arr

                })
                //   console.log(res)
            }
        })
    },

    // 获取店铺的地址信息
    getShopAddressInfo() {
        wx.showLoading({
          title: '数据加载中',
        })
        var QQMapWX = require('../../utils/qqmap-wx-jssdk1.1/qqmap-wx-jssdk');
        let baseUrl = getbaseUrl()+"/image/"
        this.setData({
            baseUrl:baseUrl
        })
        let qqmapsdk = new QQMapWX({
            key: 'AVLBZ-OBU66-J2VS6-EBTPX-RX4H7-QDFI4'
        });

        qqmapsdk.getSuggestion({
            keyword: "蜜雪冰城",
            region: "南宁市",
            page_size: 20,
            success: (res) => {
                // console.log(res.data)
                let newArr = []
                //   console.log(res)
                if(res.status==0){
                    res.data.forEach(item => {
                        // console.log(item)
                        newArr.push({
                            latitude: item.location.lat,
                            longitude: item.location.lng,
                            storeName:item.title,
                            storePhone:"19177131845",
                            id: parseInt(item.id),
                            title: item.address,
                            width: 50,
                            height: 50,
                            iconPath:baseUrl+"yao.png"
                        })
                    })
                    this.setData({
                        "map.marker": newArr
                    })
                    wx.hideLoading()
                }
               
            },
            fail: (res) => {
                this.setData({
                    "map.marker": []
                })
            },
        })



        // let res = requestUtil({
        //     url: "/shop/getShopAddress",
        //     methed: 'get'
        // })
        // res.then(({
        //     data
        // }) => {
        //     let newArr = []
        //     //   console.log(data.data)
        //     data.data.forEach(item => {
        //         newArr.push({
        //             latitude: item.latitude,
        //             longitude: item.longitude,
        //             id: item.id,
        //             title: item.shopAddress,
        //             width: 50,
        //             height: 50
        //         })
        //     })
        //     console.log(newArr)
        //     this.setData({
        //         "map.marker": newArr
        //     })
        // })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserAddress()
        this.getShopAddressInfo()
        wx.getSystemInfo({
            success: (result) => {
                //   console.log(result)
                let hei = result.windowHeight / 16
                // console.log(hei)
                this.setData({
                    hei: hei
                })
            },
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})